export class FormatUtil {
  /**
   * Format bytes to human readable string
   * @param bytes
   * @param decimals
   */
  static formatBytes(bytes: bigint, decimals = 2): string {
    if (bytes === BigInt(0)) return '0 Bytes';

    const k = 1024;
    const dm = decimals < 0 ? 0 : decimals;
    const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    const i = Math.floor(Math.log(Number(bytes)) / Math.log(k));

    return (
      parseFloat((Number(bytes) / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
    );
  }
}
